#!/bin/bash

# 设置基础URL
BASE_URL="http://localhost:8080/api/dynamic-beans"

echo "=== Spring Boot 动态Bean注册测试 ==="
echo

# 1. 注册一个简单的Bean
echo "1. 注册HelloService Bean..."
curl -X POST $BASE_URL/register \
  -H "Content-Type: application/json" \
  -d '{
    "beanName": "helloService",
    "className": "com.example.dynamic.HelloService",
    "sourceCode": "package com.example.dynamic;\n\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class HelloService {\n    public String sayHello(String name) {\n        return \"Hello, \" + name + \"!\";\n    }\n    \n    public String getMessage() {\n        return \"This is a dynamic bean!\";\n    }\n    \n    @Override\n    public String toString() {\n        return \"HelloService[dynamic]\";\n    }\n}"
  }'
echo
echo

# 2. 查询Bean信息
echo "2. 查询Bean信息..."
curl $BASE_URL/helloService
echo
echo

# 3. 调用Bean方法
echo "3. 调用sayHello方法..."
curl -X POST $BASE_URL/helloService/invoke/sayHello \
  -H "Content-Type: application/json" \
  -d '{
    "args": ["World"]
  }'
echo
echo

# 4. 调用无参方法
echo "4. 调用getMessage方法..."
curl -X POST $BASE_URL/helloService/invoke/getMessage \
  -H "Content-Type: application/json" \
  -d '{}'
echo
echo

# 5. 列出所有Bean
echo "5. 列出所有Bean..."
curl $BASE_URL/list
echo
echo

# 6. 注册Calculator Bean（自动生成名称）
echo "6. 注册Calculator Bean..."
curl -X POST $BASE_URL/register \
  -H "Content-Type: application/json" \
  -d '{
    "className": "com.example.dynamic.Calculator",
    "sourceCode": "package com.example.dynamic;\n\npublic class Calculator {\n    public int add(int a, int b) {\n        return a + b;\n    }\n    \n    public int multiply(int a, int b) {\n        return a * b;\n    }\n}"
  }'
echo
echo

# 7. 测试Calculator的add方法
echo "7. 测试Calculator的add方法..."
curl -X POST $BASE_URL/calculator/invoke/add \
  -H "Content-Type: application/json" \
  -d '{
    "args": [5, 3]
  }'
echo
echo

# 8. 移除Bean
echo "8. 移除HelloService Bean..."
curl -X DELETE $BASE_URL/helloService
echo
echo

echo "=== 测试完成 ==="